package org.altervista.cp.microkernel.persistence.spi;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class FieldIntrospector<T> {

	public FieldIntrospector(Class<? extends T> c) {
		setFieldNames(0, 0, 0, c.getDeclaredFields());
		_c = c;
	}

	private void setFieldNames(int i, int j, int k, Field[] fields) {
		if (i == fields.length) {
			if (k == 0)
				throw new IllegalArgumentException(
						"One id field must be supplied at least");
			_fields = new Field[j];
			_idFields = new Field[k];
		} else {
			String fn = fields[i].getName();
			if (Modifier.isPrivate(fields[i].getModifiers())
					&& fn.startsWith("_")) {
				fields[i].setAccessible(true);
				if (fn.startsWith("_pk_")) {
					setFieldNames(i + 1, j, k + 1, fields);
					_idFields[j] = fields[i];
				} else {
					setFieldNames(i + 1, j + 1, k, fields);
					_fields[j] = fields[i];
				}
			} else {
				setFieldNames(i + 1, j, k, fields);
			}

		}
	}

	protected Class<? extends T> _c;
	protected Field[] _fields;
	protected Field[] _idFields;

}
